<template>
  <div style="height: 100%">
    <el-container style="min-height: 100vh">
      <el-aside
        :width="sideWidth + 'px'"
        style="background-color: rgb(238, 241, 246);
      box-shadow: 2px 0 6px rgb(0 21 41);">
        <Aside :isCollapse="isCollapse" :logoTextShow="logoTextShow"></Aside>
      </el-aside>

      <el-container>
        <el-header style="border-bottom: 1px solid #ccc">
          <Header
            :collapseBtnClass="collapseBtnClass"
            :collapse="collapse"
            :user="user"
          ></Header>
        </el-header>

        <el-main>
          <!--表示当前页面的子路由会在<router-view>展示-->
          <router-view @refreshUser="getUser"></router-view>
          
        </el-main>

      </el-container>
    </el-container>
  </div>
</template>
<style>
.el-header {
  background-color: #b3c0d1;
  color: #333;
  line-height: 60px;
}

.el-aside {
  color: #333;
}
</style>

<script>
// @ is an alias to /src
import HelloWorld from "@/components/HelloWorld.vue";
import request from "@/utils/request";
import Aside from "@/components/Aside.vue";
import Header from "@/components/Header.vue";


export default {
  name: "HomeView",
  components: {
    HelloWorld,
    Aside,
    Header,
  },
  created(){
    // 从后台获取最新的User数据
    this.getUser()
  },
  data() {
    return {
      collapseBtnClass: "el-icon-s-fold",
      isCollapse: false,
      sideWidth: 200,
      logoTextShow: true,
      user:{}
    };
  },


  methods: {
   
    collapse() {
      //点击收缩按钮触发
      this.isCollapse = !this.isCollapse;
      if (this.isCollapse) {
        this.sideWidth = 64;
        this.collapseBtnClass = "el-icon-s-unfold";
        this.logoTextShow = false;
      } else {
        //点击展开按钮展开
        this.sideWidth = 200;
        this.collapseBtnClass = "el-icon-s-fold";
        this.logoTextShow = true;
      }
    },
    getUser(){
      let username = localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")).username : ""
      if (username) {
        // 从后台获取User数据
        this.request.get("/user/username/" + username).then(res => {
          // 重新赋值后台的最新User数据
          this.user = res.data
        })
      }
    }
  },
};
</script>
